package com.hshx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hshx.client.UsersClient;
import com.hshx.dao.GrantDao;
import com.hshx.dao.SalaryDao;
import com.hshx.pojo.Grant;
import com.hshx.pojo.Salary;
import com.hshx.pojo.Users;
import com.hshx.service.GrantService;
import com.hshx.vo.SalaryVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class GrantServiceImpl extends ServiceImpl<GrantDao, Grant> implements GrantService {
    @Resource
    private GrantDao grantDao;
    @Resource
    private SalaryDao salaryDao;

    @Resource
    private UsersClient usersClient;

    @Override
    public HashMap<String, Object> getSalaryVo1(int page, int limit,String name) {
        HashMap<String,Object> salaryVoMap=new HashMap<>();
        List<SalaryVo> salaryVoList = new ArrayList<>();
        //发放编号,用户姓名,查询标准表【用户中带有薪资标准id,发放表中含有用户id】
        Page grantPage= PageHelper.startPage(page,limit);
        Users usersID = usersClient.getUserByUsername(name);
        if(usersID==null) {
            return null;
        }
        List<Grant>  grantList = grantDao.selectList(new QueryWrapper<Grant>().eq("grant_users",usersID.getUsers_id()));
        if(grantList==null||grantList.size()==0){
            return null;
        }
        //遍历发放记录集合将每个记录中的用户主键取出存放
        List<Grant> useGrant=grantPage.getResult();
        useGrant.forEach(grant->{
            SalaryVo salaryVo=new SalaryVo();
            salaryVo.setGrant_absent(grant.getGrant_absent());
            salaryVo.setGrant_gold(grant.getGrant_gold());
            salaryVo.setGrant_date(grant.getGrant_date());
            salaryVo.setGrant_id(grant.getGrant_id());
            salaryVo.setGrant_insurance(grant.getGrant_insurance());
            salaryVo.setGrant_overtime(grant.getGrant_overtime());
            salaryVo.setGrant_pay(grant.getGrant_pay());
            salaryVo.setGrant_performance(grant.getGrant_performance());
            salaryVo.setGrant_tax(grant.getGrant_tax());
            salaryVo.setGrant_reduce(grant.getGrant_reduce());
            salaryVo.setGrant_total(grant.getGrant_total());


            Integer usersId=grant.getGrant_users();
            if(usersId!=null){
                Users users=usersClient.getUsersById(usersId);
                if(users!=null&&users.getUsers_id()!=null&&users.getUsers_id()>0){
                    salaryVo.setUsername(users.getUsername());
                    if(users.getUsers_salary()!=null&&users.getUsers_salary()>0){
                        Salary salary=salaryDao.selectById(users.getUsers_salary());
                        if (salary!=null&&salary.getSalary_id()>0){
                            Double traffic = salary.getSalary_money_traffic();
                            Double eat = salary.getSalary_money_eat();
                            Double regular = traffic+eat;
                            salaryVo.setSalary_money_traffic(salary.getSalary_money_traffic());
                            salaryVo.setRegular(regular);
                            salaryVo.setSalary_money_eat(salary.getSalary_money_eat());
                            salaryVo.setSalary_money_basic(salary.getSalary_money_basic());
                        }else {
                            salaryVo.setSalary_money_eat(0.00);
                            salaryVo.setSalary_money_basic(0.00);
                            salaryVo.setSalary_money_traffic(0.00);
                            salaryVo.setRegular(0.00);
                        }
                    }
                }
            }
            salaryVoList.add(salaryVo);
        });

        salaryVoMap.put("code",0);
        salaryVoMap.put("data", salaryVoList);
        salaryVoMap.put("count", grantPage.getTotal());
        return salaryVoMap;
    }
    @Override
    public HashMap<String,Object> getSalaryVo(int page,int limit) {
        HashMap<String,Object> salaryVoMap=new HashMap<>();
//        List<Object> objects=

        List<SalaryVo> salaryVoList = new ArrayList<>();
        //发放编号,用户姓名,查询标准表【用户中带有薪资标准id,发放表中含有用户id】
        Page grantPage=PageHelper.startPage(page,limit);
//        int id = usersClient.
        List<Grant> grantList = grantDao.selectList(new QueryWrapper<Grant>());
        if(grantList==null||grantList.size()==0){
            return null;
        }
        //遍历发放记录集合将每个记录中的用户主键取出存放

        List<Grant> useGrant=grantPage.getResult();
        useGrant.forEach(grant->{
            SalaryVo salaryVo=new SalaryVo();
            salaryVo.setGrant_absent(grant.getGrant_absent());
            salaryVo.setGrant_gold(grant.getGrant_gold());
            salaryVo.setGrant_date(grant.getGrant_date());
            salaryVo.setGrant_id(grant.getGrant_id());
            salaryVo.setGrant_insurance(grant.getGrant_insurance());
            salaryVo.setGrant_overtime(grant.getGrant_overtime());
            salaryVo.setGrant_pay(grant.getGrant_pay());
            salaryVo.setGrant_performance(grant.getGrant_performance());
            salaryVo.setGrant_tax(grant.getGrant_tax());
            salaryVo.setGrant_reduce(grant.getGrant_reduce());
            salaryVo.setGrant_total(grant.getGrant_total());


            Integer usersId=grant.getGrant_users();
            if(usersId!=null){
                Users users=usersClient.getUsersById(usersId);
                if(users!=null&&users.getUsers_id()!=null&&users.getUsers_id()>0){
                    salaryVo.setUsername(users.getUsername());
                    if(users.getUsers_salary()!=null&&users.getUsers_salary()>0){
                        Salary salary=salaryDao.selectById(users.getUsers_salary());
                        if (salary!=null&&salary.getSalary_id()>0){
                            Double traffic = salary.getSalary_money_traffic();
                            Double eat = salary.getSalary_money_eat();
                            Double regular = traffic+eat;
                            salaryVo.setSalary_money_traffic(salary.getSalary_money_traffic());
                            salaryVo.setRegular(regular);
                            salaryVo.setSalary_money_eat(salary.getSalary_money_eat());
                            salaryVo.setSalary_money_basic(salary.getSalary_money_basic());
                        }else {
                            salaryVo.setSalary_money_eat(0.00);
                            salaryVo.setSalary_money_basic(0.00);
                            salaryVo.setSalary_money_traffic(0.00);
                            salaryVo.setRegular(0.00);
                        }
                    }
                }
            }
            salaryVoList.add(salaryVo);
        });

        salaryVoMap.put("code",0);
        salaryVoMap.put("data", salaryVoList);
        salaryVoMap.put("count", grantPage.getTotal());
        return salaryVoMap;
    }
}
